import os
import smtplib
from email import encoders
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from performance import util
import time
curr_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
root_dir = curr_dir + os.sep

def struct_email(evironment_data,data):
    username = []
    mail_host = 'smtp.37.com'
    mail_user = 'liuxiandong@37.com'
    mail_pass = '1qaz2wsx#EDC'
    sender = 'liuxiandong@37.com'
    # username.append(sender)
    # username.append(evironment_data[0])
    receivers = evironment_data[0]
    data_list = util.insert_data_2_excel(data)
    message = MIMEMultipart('related')
    alternative = MIMEMultipart('alternative')
    message.attach(alternative)
    with open(root_dir+'templates\emailreport.html', 'r',encoding='utf-8') as f:
        mail_content = f.read()
    t = time.strftime('%Y-%m-%d', time.localtime())  # 动态生成日期
    # TODO 后面需要补充各个动态参数的替换
    print(data_list)
    re_mail_content = mail_content. \
        replace('${project}', str(evironment_data[1])). \
        replace('${average_mem}', str(data_list[0])). \
        replace('${average_fps}', str(data_list[1])). \
        replace('${average_cpu}', str(data_list[2])). \
        replace('${client_version}', str(evironment_data[2])). \
        replace('${server_version}', str(evironment_data[3])). \
        replace('${devices}', str(evironment_data[4])). \
        replace('${t}', t)  # 使用替换方法动态填充参数
    # # 设置邮件信息Ò
    # 邮件内容设置
    # content_message = MIMEText(mail_content, 'html', 'utf-8')
    content_message = MIMEText(re_mail_content, 'html', 'utf-8')
    message.attach(content_message)

    # 添加logo图片插入都邮件内容中
    with open(root_dir+'\static\images\logo.png', 'rb') as f:
        logo_icon = f.read()
    # msg.add_header('content-disposition', 'attachment', filename='bud.gif')
    msg_logo = MIMEImage(logo_icon)
    msg_logo.add_header('Content-ID', '<logo>')
    message.attach(msg_logo)
    with open(curr_dir+'\内存情况.png', 'rb') as f:
        meminfo = f.read()
    msg_meminfo = MIMEImage(meminfo)
    msg_meminfo.add_header('Content-ID', '<meminfo>')
    with open(curr_dir+'\帧率情况.png', 'rb') as f:
        fpsinfo = f.read()
    msg_fpsinfo = MIMEImage(fpsinfo)
    msg_fpsinfo.add_header('Content-ID', '<fpsinfo>')
    with open(curr_dir+'\CPU占用情况.png', 'rb') as f:
        cpuinfo = f.read()
    msg_cpuinfo = MIMEImage(cpuinfo)
    msg_cpuinfo.add_header('Content-ID', '<cpuinfo>')
    message.attach(msg_meminfo)
    message.attach(msg_fpsinfo)
    message.attach(msg_cpuinfo)
    # 邮件主题
    message['Subject'] = '客户端性能测试报告'
    message['From'] = sender
    message['To'] = receivers
    with open(curr_dir+'\测试报告.xlsx', 'rb') as f:
        content = f.read()
    part1 = MIMEBase('application', 'vnd.ms-excel')
    part1.set_payload(content)
    encoders.encode_base64(part1)
    part1.add_header('content-disposition', 'attachment', filename="测试报告.xlsx")
    message.attach(part1)
    try:
        smtp_objt = smtplib.SMTP()
        smtp_objt.connect(mail_host, 25)
        smtp_objt.login(mail_user, mail_pass)
        print(receivers)
        smtp_objt.sendmail(sender, receivers, message.as_string())
        smtp_objt.quit()
    except smtplib.SMTPException as e:
        print(e)


if __name__ == '__main__':
    struct_email("liuxiandong@37.com")
